package cn.itcast.travel.util;

import cn.itcast.travel.domain.Weather;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WeatherUtil {
    public static Weather getWeather(String s){
        String host = "https://ali-weather.showapi.com";
        String path = "/area-to-weather";
        String method = "GET";
        String appcode = "f56ed0d710e34434b3f0fb51c6a58860";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("area", s);
        querys.put("areaid", "0");
        querys.put("need3HourForcast", "0");
        querys.put("needAlarm", "0");
        querys.put("needHourData", "0");
        querys.put("needIndex", "0");
        querys.put("needMoreDay", "1");

        Weather weather=new Weather();
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);

            //获取response的body
            String s1 = EntityUtils.toString(response.getEntity());

            JSONObject jsonObject = new JSONObject(s1);
            JSONObject showapi_res_body = jsonObject.getJSONObject("showapi_res_body");
            JSONObject now = showapi_res_body.getJSONObject("now");
            //System.out.println(s1);
            List<String> arr = new ArrayList<>();
            for (int i = 1; i < 8; i++) {
                arr.add((String)showapi_res_body.getJSONObject("f"+i).get("night_weather_pic"));
            }
            weather.setFutureWeather(arr);
            JSONObject aqiDetail = now.getJSONObject("aqiDetail");

            String wind_direction = (String) now.get("wind_direction");
            String area =(String) aqiDetail.get("area");
            String quality = (String)aqiDetail.get("quality");
            String sd = (String)now.get("sd");
            String weather_pic = (String)now.get("weather_pic");
            String weathers = (String)now.get("weather");
            String temperature = (String)now.get("temperature");

            weather.setArea(area);
            weather.setQuality(quality);
            weather.setSd(sd);
            weather.setTemperature(temperature);
            weather.setWeather(weathers);
            weather.setWeather_pic(weather_pic);
            weather.setWind_direction(wind_direction);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return weather;
    }

    public static void main(String[] args) {


    }
}
